package lxt.dao.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import lxt.dao.TruckDao;
/**
 * 车辆管理dao
 * @author Lit
 *
 */
@SuppressWarnings("unchecked")
public class TruckDaoImpl extends ABseDaoImpl implements TruckDao {

	/**
	 * 根据具体信息查询
	 */
	@SuppressWarnings("unchecked")
	public List<Object> loadAllTruck(String truckId, String netryTime, int offset, int length) {
		//定义hql
		StringBuffer hql = new StringBuffer("Select t,p from TruckInfo t,Parameter p where t.truckType = p.id.paramKey And p.id.paramType = ? ");
		//定义参数
		ArrayList<Object> param = new ArrayList<Object>();
		param.add("truckType");
		if(truckId!=null && !truckId.equals("")){
			hql.append(" And t.truckId like ? ");
			param.add("%"+truckId+"%");
		}
		if(netryTime!=null && !netryTime.equals("")){
			try {
				hql.append(" And t.netryTime = ? ");
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				Date date = sdf.parse(netryTime);
				param.add(date);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		
		List<Object> trucks = (List<Object>) this.queryForPage(hql.toString(), param, offset, length);
		
		return trucks;
	}
	/**
	 * 查询数量
	 */
	public int loadAllTruckCount(String truckId, String netryTime) {
		//定义hql
		StringBuffer hql = new StringBuffer("Select t,p from TruckInfo t,Parameter p where t.truckType = p.id.paramKey And p.id.paramType = ? ");
		//定义参数
		ArrayList<Object> param = new ArrayList<Object>();
		param.add("truckType");
		if(truckId!=null && !truckId.equals("")){
			hql.append(" And t.truckId like ? ");
			param.add("%"+truckId+"%");
		}
		if(netryTime!=null && !netryTime.equals("")){
			try {
				hql.append(" And t.netryTime = ? ");
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				Date date = sdf.parse(netryTime);
				param.add(date);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		 
		return this.getAllRowCount(hql.toString(), param);
	}
	
}
